package com.sangmo.fts.api.storage;

import com.sangmo.boot.framework.api.utils.ApiResponse;
import com.sangmo.boot.framework.common.security.meta.Access;
import com.sangmo.boot.framework.data.filter.PageResult;
import com.sangmo.boot.framework.data.filter.ValueFilter;
import com.sangmo.boot.framework.data.json.ExtViewHelper;
import com.sangmo.boot.framework.plugin.dataview.data.DataViewPageSearch;
import com.sangmo.fts.basic.model.FileUpload;
import com.sangmo.fts.process.inventory.interfaces.IfContractCreation;
import com.sangmo.fts.process.storage.interfaces.CsfContractCreation;
import com.sangmo.fts.process.storage.model.CsfContract;
import com.sangmo.fts.process.storage.service.CsfContractService;
import com.sangmo.fts.restructure.meta.TrainingApi;
import com.sangmo.fts.restructure.meta.TrainingProcess;
import com.sangmo.fts.training.model.TrainingHistory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.HashMap;

@TrainingApi
@Controller
@RequestMapping("/csfContract/v1")
@Access("csfContract")
public class CsfContractApi {

    @Autowired
    private CsfContractService csfContractService;

    @Access(":create")
    @PostMapping(value = "/{trainingId}/create")
    public @ResponseBody
    ApiResponse<CsfContract> create(@PathVariable("trainingId") Long trainingId, @RequestBody @Valid CsfContractCreation obj) {
        obj.setTrainingId(trainingId);
        return ApiResponse.make(csfContractService.create(obj));
    }

    @Access(":remove")
    @PostMapping("/{trainingId}/{id}/remove")
    public @ResponseBody ApiResponse remove(@PathVariable("trainingId") Long trainingId, @PathVariable("id") Long id) {
        csfContractService.deleteById(id);
        return ApiResponse.make();
    }

    @Access(":read")
    @PostMapping("/{trainingId}/search")
    public @ResponseBody ApiResponse<PageResult<CsfContract>> search(@PathVariable("trainingId") Long trainingId, @RequestBody @Valid DataViewPageSearch<HashMap> ps) {
        ExtViewHelper.setGroup("CsfContract.search", 1);
        ps.getFilters().add(new ValueFilter("trainingId", ValueFilter.OP_EQ, trainingId));
        return ApiResponse.make(csfContractService.search(ps));
    }

    @TrainingProcess(node = TrainingHistory.CsfProcessNodes.CSF_CONTRACT_BUYER_CONFIRM_1)
    @Access(":buyerConfirm")
    @PostMapping({"/{trainingId}/{id}/buyer/confirm"})
    @ResponseBody
    public ApiResponse buyerConfirm(@PathVariable("trainingId") Long trainingId, @PathVariable("id") Long id) {
        csfContractService.buyerConfirm(id);
        return ApiResponse.make();
    }

    @TrainingProcess(node = TrainingHistory.CsfProcessNodes.CSF_CONTRACT_SELLER_CONFIRM_2)
    @Access(":sellerConfirm")
    @PostMapping({"/{trainingId}/{id}/seller/confirm"})
    @ResponseBody
    public ApiResponse sellerConfirm(@PathVariable("trainingId") Long trainingId, @PathVariable("id") Long id) {
        csfContractService.sellerConfirm(id);
        return ApiResponse.make();
    }

    @Access(":selection")
    @PostMapping({"/{trainingId}/selection"})
    @ResponseBody
    public ApiResponse<PageResult<CsfContract>> selection(@PathVariable("trainingId") Long trainingId, @RequestBody @Valid DataViewPageSearch<HashMap> ps) {
        ExtViewHelper.setGroup("CsfContract.selection", 1);
        ps.getFilters().add(new ValueFilter("trainingId", ValueFilter.OP_EQ, trainingId));
        return ApiResponse.make(csfContractService.selection(ps));
    }
}
